<?php

namespace app\admin\controller\hour;

use app\common\controller\Backend;
use app\admin\model\AuthGroupAccess;
use app\admin\model\Admin;
use think\Db;
/**
 * 日常管理
 *
 * @icon fa fa-circle-o
 */
class ClassHour extends Backend
{

    /**
     * ClassHour模型对象
     * @var \app\admin\model\ClassHour
     */
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ClassHour;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $AuthGroupAccessInfo = AuthGroupAccess::where('uid',$this->auth->id)->find();
            if(empty($AuthGroupAccessInfo))$this->error('登录错误');
            $and_where = [];
            if($AuthGroupAccessInfo['group_id'] == 12){
                $and_where['admin_id'] = $this->auth->id;
            }
            $list = $this->model
                ->with(['admin','course'])
                ->where($and_where)
                ->where('class_hour.type',1)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->first_trial_admin_name = Admin::where('id',$row->first_trial_admin_id)->value('nickname');
                $row->end_trial_admin_name = Admin::where('id',$row->end_trial_admin_id)->value('nickname');
                $row->visible(['id','substitute_admin_id','course','admin','course_type_id','select_time','num',
                    'remarks','type','status','reject_reason','first_trial_reject_reason','first_trial_admin_name','first_trial_time',
                    'end_trial_reject_reason','end_trial_time','end_trial_admin_name','create_time']);

            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                Db::startTrans();
                try {
                    $validate = $this->validate($params, 'ClassHour.add');
                    if ($validate !== true) exception($validate);
                    $params['admin_id'] = $this->auth->id;
                    $result = $this->model->allowField(true)->save($params);
                    if(!$result)exception('添加失败');
                    Db::commit();
                } catch (\Throwable $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }


    /**
     * 提交审核
     */
    public function to_examine($ids)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            Db::startTrans();
            try {
                $res = $row->save(array('status'=>'2'));
                if (!$res)exception('提交成功');
                Db::commit();
            }  catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }

        return $this->view->fetch();
    }

}
